﻿using Collection.Entities;
using Super.Collection.Core.Providers;
using Super.Collection.Core.Providers.Sports;
using Super.Collection.Core.Providers.Welfare;
using Super.Collection.Entities;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Super.Collection.Console
{
    class Program
    {
        static void Main(string[] args)
        {
			ChinaWelfareLotteryNet net = new ChinaWelfareLotteryNet();
			net.GetLatestLotteryDraw();


			return;

            string url = "http://baidu.lecai.com/lottery/draw/list/1?lottery_type=1&page={0}&ds=2012-12-31&de=2013-03-31";
            IProvider provider = new LeCaiNet();
            System.Console.WriteLine("开奖日期————期号————红球————篮球————超级星期天");
            List<Announcement> lotteryReports;
            for (var i = 1; i <= 30; i++)
            {
                lotteryReports = provider.GetAnnouncements(string.Format(url, i.ToString()), Encoding.Default);
                foreach (Announcement lotteryReport in lotteryReports)
                {
                    Write(lotteryReport);
                    //Save(lotteryReport);
                }
            }


            /*
            var urls = (NameValueCollection)ConfigurationManager.GetSection("welfare/doubleColor");
            IProvider provider = new ChinaWelfareLotteryNet();
            System.Console.WriteLine("开奖日期————期号————红球————篮球————超级星期天");
            List<LotteryReport> lotteryReports;
            for (var i = 1; i <= 30; i++)
            {
                lotteryReports = provider.GetLotteryReport(string.Format(urls[1], i.ToString()), Encoding.Default);
                foreach (LotteryReport lotteryReport in lotteryReports)
                {
                    Write(lotteryReport);
                    Save(lotteryReport);
                }
            }
             */
            System.Console.Read();
        }


        static void Write(Announcement lotteryReport)
        {
            System.Console.WriteLine(string.Format("{0}————{1}————{2}————{3}"
                   , lotteryReport.LotteryBriefing.LotteryDates.ToString("dd-MM-yy")
                   , lotteryReport.LotteryBriefing.SerialNumber
                   , string.Format("{0}|{1}|{2}|{3}|{4}|{5}", lotteryReport.LotteryNumbers[0].Number
                   , lotteryReport.LotteryNumbers[1].Number, lotteryReport.LotteryNumbers[2].Number
                   , lotteryReport.LotteryNumbers[3].Number, lotteryReport.LotteryNumbers[4].Number
                   , lotteryReport.LotteryNumbers[5].Number), lotteryReport.LotteryNumbers[6].Number
                   , lotteryReport.LotteryNumbers.Count > 7 ? lotteryReport.LotteryNumbers[7].Number.ToString() : string.Empty));
        }

        static void Save(Announcement lotteryReport)
        {
            ServiceFactory.BulletinService.Insert(lotteryReport.LotteryBriefing);
            lotteryReport.LotteryBriefing = ServiceFactory.BulletinService.Get(lotteryReport.LotteryBriefing.SerialNumber
                , lotteryReport.LotteryBriefing.LotteryTicketId, lotteryReport.LotteryBriefing.Provider);
            foreach (var lotteryDetails in lotteryReport.LotteryDetails)
            {
                lotteryDetails.BriefingId = lotteryReport.LotteryBriefing.Id;
                ServiceFactory.LotteryDetailsService.Insert(lotteryDetails);
            }

            foreach (var lotteryNumbers in lotteryReport.LotteryNumbers)
            {
                lotteryNumbers.BriefingId = lotteryReport.LotteryBriefing.Id;
                ServiceFactory.LotteryNumbersService.Insert(lotteryNumbers);
            }
        }
    }
}
